from PIL import Image
from file_dir.file_list import get_all_files
from file_dir.file_operator import mkdir

output_path = r'D:/data/output/'
pre_fix = '套图01'
def res_image(img):
    original_image = Image.open(img)
    relative_path = img[img.rfind(pre_fix):]
    relative_dir = img[img.rfind(pre_fix):img.rfind('\\')]
    # 设置缩放比例
    resize_ratio =0.3  # 例如，这里设置为50%的大小
    wideth = int(original_image.size[0]*resize_ratio)
    height = int(original_image.size[1]*resize_ratio)
    # 缩放图片
    resized_image = original_image.resize((wideth,height))
    mkdir(output_path + relative_dir)
    # 保存缩放后的图片
    resized_image.save(output_path + relative_path)

def batch_resize_by_dirs(dirs):
    for dir in dirs:
        file_list = get_all_files(dir)
        for file_name in file_list:
            res_image(file_name)

if __name__ == '__main__':
    # path = r'E:\Enterainment\套图01\8002'
    # file_list = get_all_files(path)
    # for file_name in file_list:
    #     res_image(file_name)
    path_prefix = r'E:\\Enterainment\\套图01\\'
    paths = [path_prefix + '8004',
             path_prefix + '7009(1)',
             ]
    batch_resize_by_dirs(paths)
